/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 31, 2009
 * Time: 10:35:24 AM
 * To change this template use File | Settings | File Templates.
 */
import java.util.*;
import org.jdom.Element;

// Referenced classes of package dk.brics.dsd:
//            Rule, Select, Field, FieldEvaluationException,
//            Context, Schema, Boolexp, Validator

class UniqueRule extends Rule
{

    UniqueRule(Element element, Schema schema)
    {
label0:
        {
            selects = new ArrayList();
            Iterator iterator = element.getChildren().iterator();
            do
            {
                if(!iterator.hasNext())
                    break label0;
                Element element1 = (Element)iterator.next();
                if(!Schema.isDSDElement(element1))
                    continue;
                if(!element1.getName().equals("select"))
                    break;
                selects.add(new Select(element1, schema));
            } while(true);
            selects.add(new Select(element, schema));
        }
        key = element.getAttributeValue("key");
        if(key == null)
            key = "";
    }

    boolean skipProcessing(Context context)
    {
        if(!this_check_performed)
        {
            Iterator iterator = selects.iterator();
            do
            {
                if(!iterator.hasNext())
                    break;
                if(!((Select)iterator.next()).exp.containsThis(context))
                    continue;
                exp_contains_this = true;
                break;
            } while(true);
            this_check_performed = true;
            return false;
        } else
        {
            return !exp_contains_this;
        }
    }

    void checkUniqueness(Context context, Set set, Map map, Element element)
    {
        String s = context.e.getNamespaceURI();
        if(s.equals("http://www.brics.dk/DSD/2.0/error") || s.equals("http://www.brics.dk/DSD/2.0/meta"))
            return;
        Iterator iterator = selects.iterator();
        do
        {
            if(!iterator.hasNext())
                break;
            Select select = (Select)iterator.next();
            if(select.exp.evaluateMemo(context) == 1)
            {
                boolean flag = true;
                ArrayList arraylist = new ArrayList();
                arraylist.add(key);
                for(Iterator iterator2 = select.fields.iterator(); iterator2.hasNext();)
                    try
                    {
                        String s1 = ((Field)iterator2.next()).evaluate(context, element);
                        arraylist.add(s1);
                    }
                    catch(FieldEvaluationException fieldevaluationexception)
                    {
                        context.v.invalid2(new Context(context.this_e, context.v), "uniqueness field evaluation failed at element " + Validator.getElementName(context.e) + ", " + fieldevaluationexception.getMessage(), toXML(context));
                        flag = false;
                    }

                if(set.contains(arraylist) && flag)
                {
                    context.v.invalid2(new Context(context.this_e, context.v), "uniqueness rule not satisfied for element " + Validator.getElementName(context.e) + " with key value " + arraylist.subList(1, arraylist.size()), toXML(context));
                    flag = false;
                }
                if(flag)
                {
                    set.add(arraylist);
                    Object obj = (Set)map.get(arraylist);
                    if(obj == null)
                    {
                        obj = new HashSet();
                        map.put(arraylist, obj);
                    }
                    Validator.debug("adding key (" + context.e + ", " + arraylist + ")");
                    ((Set) (obj)).add(context.e);
                }
            }
        } while(true);
        for(Iterator iterator1 = context.e.getChildren().iterator(); iterator1.hasNext(); checkUniqueness(new Context((Element)iterator1.next(), context.this_e, context.v), set, map, element));
    }

    Element toXML(Context context)
    {
        Element element = new Element("unique", "http://www.brics.dk/DSD/2.0");
        if(key != null)
            element.setAttribute("key", key);
        for(Iterator iterator = selects.iterator(); iterator.hasNext(); element.addContent(((Select)iterator.next()).toXML(context)));
        return element;
    }

    List selects;
    String key;
    boolean this_check_performed;
    boolean exp_contains_this;
}

